from rest_framework.decorators import action
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from meiduo_admin.utils import PageNum
from orders.models import OrderInfo

from meiduo_admin.serialziers.orders import OrderSerializer


class OrderView(ModelViewSet):
    serializer_class = OrderSerializer
    queryset = OrderInfo.objects.all()
    # 分页
    pagination_class = PageNum
    permission_classes = [IsAdminUser]

    @action(methods=['put'], detail=True)
    def status(self, request, pk):
        # 获取订单对象
        order = self.get_object()
        # 获取要修改的状态值
        status = request.data.get('status')
        # 修改订单状态
        order.status = status
        order.save()
        # 返回结果
        ser = self.get_serializer(order)
        return Response({
            'order_id': order.order_id,
            'status': status
        })



